package cn.yz.service.impl;

import cn.yz.dto.AccountBillDto;
import cn.yz.dto.AccountBillPageDto;
import cn.yz.entity.AccountBill;
import cn.yz.mapper.AccountBillMapper;
import cn.yz.result.Result;
import cn.yz.service.AccountBillService;
import cn.yz.vo.AccountBillVo;
import cn.yz.vo.PageVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 账单表(AccountBill)服务实现类
 *
 * @author yangzhen
 * @since 2025-07-14 14:59:10
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AccountBillServiceImpl extends ServiceImpl<AccountBillMapper, AccountBill> implements AccountBillService {

    private final IdWorker idWorker;

    @Override
    public Result<PageVo<AccountBillVo>> queryPage(AccountBillPageDto pageDto) {
        try {
            // 构造分页与查询条件
            Page<AccountBill> page = new Page<>(pageDto.getPage(), pageDto.getSize());
            LambdaQueryWrapper<AccountBill> wrapper = new LambdaQueryWrapper<>();
            // TODO: 根据 pageDto 添加过滤条件，例如 wrapper.eq(...) 等

            // 分页查询
            IPage<AccountBill> result = this.page(page, wrapper);

            // 转换为 PageVo 并返回
            return Result.success(PageVo.from(result, bill -> bill.toVo(AccountBillVo.class)));
        } catch (Exception e) {
            return Result.error("分页查询失败: " + e.getMessage());
        }
    }

    @Override
    public Result<List<AccountBillVo>> queryList(AccountBillDto accountBillDto) {
        return null;
    }

    @Override
    public Result<Void> insert(AccountBillDto accountBillDto) {
        try {
            AccountBill entity = accountBillDto.toEntity(AccountBill.class);
            entity.setId(String.valueOf(IdWorker.getId())); // 使用 IdWorker 生成唯一ID
            int inserted = baseMapper.insert(entity);
            return inserted > 0 ? Result.success() : Result.error("新增失败");
        } catch (Exception e) {
            return Result.error("新增失败: " + e.getMessage());
        }
    }

    @Override
    public Result<Void> update(AccountBillDto accountBillDto) {
        try {
            AccountBill entity = accountBillDto.toEntity(AccountBill.class);
            int updated = baseMapper.updateById(entity);
            return updated > 0 ? Result.success() : Result.error("修改失败");
        } catch (Exception e) {
            return Result.error("修改失败: " + e.getMessage());
        }
    }

    @Override
    public Result<Void> delete(String id) {
        try {
            AccountBill entity = baseMapper.selectById(id);
            if (entity == null) {
                return Result.error("无效id，未找到记录");
            }
            int deleted = baseMapper.deleteById(id);
            return deleted > 0 ? Result.success() : Result.error("删除失败");
        } catch (Exception e) {
            return Result.error("删除失败: " + e.getMessage());
        }
    }
}
